﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommonTagTime;
using CommonTagTime.Business;
using CommonTagTime.ModelObject;

public partial class masDefault : System.Web.UI.MasterPage
{
    public SitePage sp = new SitePage();
    //------------------------------------------------
    //METHODS
    //------------------------------------------------
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ShowBreadcrum();
        }
    }


    private void ShowBreadcrum()
    {
        string strPageName = Request.Path.ToString().Replace(".asxp", "");

        string strLevel1 = string.Empty;
        string strLevel2 = string.Empty;

        if (strPageName.Contains("/Product"))
        {
            //Level 1
            ltrBreadcrum.Text = string.Format(@"<a href=""/products"" class=""last"">{0}</a>", sp.GetLang("Products"));

            //Level 2
            if (clsCheck.QueryString("cateId", enmDataType._Int))
            {
                busCategory busCategory = new busCategory();
                Category objCategory = busCategory.GetByID(Convert.ToInt32(clsString.QueryString("cateId")));
                if (objCategory != null)
                {
                    ltrBreadcrum.Text += string.Format(@"{0}", objCategory.Name);
                }
            }
            else if (clsCheck.QueryString("pId", enmDataType._Int))
            {
                busCategory objBusCate = new busCategory();
                Category objCategory = objBusCate.GetByProductID(Convert.ToInt32(clsString.QueryString("pId")));

                ltrBreadcrum.Text += string.Format(@"<a href=""/danh-muc/{0}/{1}"" class=""last"">{2}</a>", clsText.ToSEO(objCategory.Name), objCategory.CategoryID, objCategory.Name);
            }
        }
        else if (strPageName.Contains("/Shop"))
        {
            int catePara = clsQueryString.GetInt("cateId");
            int countryId = clsQueryString.GetInt("countryId");
            ltrBreadcrum.Text = string.Format(@"<a href=""/shop"">Shop</a>");
            //get paramater 
            busCategory objBusCate = new busCategory();
            string pathID = objBusCate.GetCategoryPathId(catePara);
            string[] arrCare = pathID.Split('/');
            if (arrCare.Length > 0)
            {
                int cateID = 0;
                for (int i = 0; i < arrCare.Length; i++)
                {
                    cateID = clsConvert.Int(arrCare[i].ToString());
                    if (cateID > 0)
                    {
                        Category objCate = objBusCate.GetByID(cateID);
                        if (objCate != null && objCate.Level != 1)
                        {
                            if (countryId < 1 && i == arrCare.Length - 1)
                                ltrBreadcrum.Text += string.Format(@"{0}", objCate.Name);
                            else
                                ltrBreadcrum.Text += string.Format(@"<a href=""/shop/{0}/{1}/0"">{2}</a>", clsText.ToSEO(objCate.Name), objCate.CategoryID, objCate.Name);
                        }
                    }
                }
                if (countryId > 0)
                {
                    busCountry objBusCountry = new busCountry();
                    Country objCountry = objBusCountry.GetById(countryId);
                    if (objCountry != null)
                    {
                        ltrBreadcrum.Text += string.Format(@"{0}", objCountry.CountryName);
                    }
                }
            }

        }
        else if (strPageName.Contains("/videolist"))
        {
            int catePara = clsQueryString.GetInt("cateId");
            if (catePara < 1)
                ltrBreadcrum.Text = string.Format(@"Video");
            else ltrBreadcrum.Text = string.Format(@"<a href=""/video"">Video</a>");
            busCategory objBusCate = new busCategory();
            string pathID = objBusCate.GetCategoryPathId(catePara);
            string[] arrCare = pathID.Split('/');
            if (arrCare.Length > 0)
            {
                int cateID = 0;
                for (int i = 0; i < arrCare.Length; i++)
                {
                    cateID = clsConvert.Int(arrCare[i].ToString());
                    if (cateID > 0)
                    {
                        Category objCate = objBusCate.GetByID(cateID);
                        if (objCate != null && objCate.Level != 1)
                        {
                            if (i == arrCare.Length - 1)
                                ltrBreadcrum.Text += string.Format(@"{0}", objCate.Name);
                            else
                                ltrBreadcrum.Text += string.Format(@"<a href=""/video/{0}/{1}"">{2}</a>", clsText.ToSEO(objCate.Name), objCate.CategoryID, objCate.Name);
                        }
                    }
                }
            }

        }
        else if (strPageName.Contains("/StepByStep") || strPageName.Contains("/StepByStepDetail"))
        {
            ltrBreadcrum.Text = string.Format(@"Step by Step");
        }
        else if (strPageName.Contains("/MSDS"))
        {
            ltrBreadcrum.Text = string.Format(@"MSDS");
        }
        else if (strPageName.Contains("/News"))
        {
            ltrBreadcrum.Text = sp.GetLang("News");
        }
        else if (strPageName.Contains("/ContactUs"))
        {
            ltrBreadcrum.Text = string.Format(@"Liên hệ");
        }
        else if (strPageName.Contains("/Sitemap"))
        {
            ltrBreadcrum.Text = string.Format(@"Sơ đồ website");
        }
    }

    protected void btnEnglish_Click(object sender, EventArgs e)
    {
        Session["Language"] = "en-US";
        Response.Redirect(GetUrl());
    }

    protected void btnVietnam_Click(object sender, EventArgs e)
    {
        Session["Language"] = "vi-VN";
        Response.Redirect(GetUrl());
    }

    private string GetUrl()
    {
        string str = Request.RawUrl;
        if (Request.Url.AbsolutePath == "/ContentPage.aspx" || Request.Url.AbsolutePath == "/NewsDetail.aspx")
        {
            if (str.Contains("url"))
            {
                str = str.Substring(0, str.IndexOf("?url"));
            }
            else if (str.Contains("id"))
            {
                str = str.Substring(0, str.IndexOf("?id"));
            }
        }

        return str;
    }
}
